﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _5_1_Array
{
    class Program
    {
        static int Fibonacci(int n)
        {
            if (1 == n || 2 == n)
                return 1;
            return Fibonacci(n - 1) + Fibonacci(n - 2);
        }

        static void PrintArray(int[] arr)
        {
            foreach (int item in arr)
            {
                Console.Write(item + "  ");
            }
            Console.WriteLine();
        }
        static void MaoPao(int[] arr)
        {
            //Console.WriteLine("排序前：");
            //PrintArray(arr);
            
            int temp = arr[0];
            for (int i = 0; i < arr.Length; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    if (arr[i] < arr[j])
                    {
                        temp = arr[j];
                        arr[j] = arr[i];
                        arr[i] = temp;
                    }
                }
            }
            
            //1
            //Array.Sort<int>(arr);
            //2
            //Array.Sort(arr);
            
           // Console.WriteLine("排序后：");
            //PrintArray(arr);

            //2
            //Array.Sort(arr);
        }

        static void TwoArray()
        {

            string[,,] threeArray = new string[3, 4, 5];

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    for (int k = 0; k < 5; k++)
                    {
                        threeArray[i, j, k] = i + "-" + j + "-" + k;
                        Console.Write(threeArray[i, j, k] + "   ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("----------------------------------");
            }

            int[] ai = new int[2] {0,1 };

            //4行int     1，2，3，4
            //5行string  3，2，3，2，3

            int[,] arrayInt = { {0,1,2,3 }, {5,6,7,8 }, {9,10,11,12 } };

            int[][] arrayInt2 = { new int[2] { 0, 1 }, new int[] { 2, 3, 4 }, new int[] { 5, 6, 7, 8 } };
            int[][] arrayInt1 = new int[3][] { new int[2] {0,1 }, new int[3] {2,3,4 }, new int[4] {5,6,7,8 }};

            Console.WriteLine(arrayInt1.Length);
            for (int i = 0; i < arrayInt1.Length; i++)
            {
                arrayInt1[i] = new int[i + 2];
            }

            for (int i = 0; i < arrayInt1.Length; i++)
            {
                for (int j = 0; j < arrayInt1[i].Length; j++)
                {
                    Console.Write("[{0}][{1}] = {2}  ", i, j, arrayInt1[i][j]);
                }
                Console.WriteLine();
            }

            Console.ReadKey();

            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(arrayInt[0,i]);
            }

            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Console.Write("[{0}][{1}] = {2}  ",i,j, arrayInt[i,j]);
                }
                Console.WriteLine();
            }
            Console.ReadKey();



            string[,] strArr = new string[2,3];

            string[][] strArr1 = new string[3][];
            strArr1[0] = new string[2];
            strArr1[0][0] = "string 0 0";
            strArr1[0][1] = "string 0 1";

            for (int i = 0; i < strArr1.Length; i++)
            {
                strArr1[i] = new string[i+2];
            }


            for (int i = 0; i < strArr1.Length; i++)
            {
                for (int j = 0; j < strArr1[i].Length; j++)
                {
                    strArr1[i][j] = "string " + i + " " + j;
                }
            }

            for (int i = 0; i < strArr1.Length; i++)
            {
                for (int j = 0; j < strArr1[i].Length; j++)
                {
                    Console.Write(strArr1[i][j] + " ");
                }
                Console.WriteLine();
            }

            Console.WriteLine(strArr1[0][0]);
            Console.WriteLine(strArr1[0]);
            Console.WriteLine(strArr1);

            Program p = new Program();
            Console.WriteLine(p);

            Console.ReadKey();
        }

        public override string ToString()
        {
            return "My Class Program";
        }

        static void ChangeInt(int i)
        {
            i++;
        }

        static void Main(string[] args)
        {
            int ti = 1245;
            Console.WriteLine(ti);
            ChangeInt(ti);
            Console.WriteLine(ti);

            int[] arrInt1 = { 5, 2, 9, 4, 7, 1, 1, 3, 8, 10, 100, 0 };
            PrintArray(arrInt1);
            MaoPao(arrInt1);
            PrintArray(arrInt1);

            Console.ReadKey();

            TestArray3();

            TestArray2();

            TestArray1();

            TestArray();

            TwoArray();


            int[] fib = new int[20];
            fib[0] = 1;
            fib[1] = 1;
            for (int i = 2; i < fib.Length; i++)
            {
                fib[i] = fib[i - 1] + fib[i - 2];
            }
            for (int i = 0; i < fib.Length; i++)
            {
                Console.Write(fib[i] + "  ");
            }
            Console.WriteLine();
            for (int i = 0; i < fib.Length; i++)
            {
                Console.Write(Fibonacci(i+1) + "  ");
            }
            Console.ReadKey();
            const int arrSize = 5;


            

            int[] arrInt2 = new int[] { 1, 2, 3, 4 };
            SortArray(arrInt2);

            int[] arrInt3 = new int[3] { 1, 2, 3};
            int[] arrInt4 = new int[arrSize];
            for (int i = 0; i < arrInt4.Length; i++)
            {
                Console.WriteLine(arrInt4[i]);
            }

            int[] arrInt5 = new int[10];
            for (int i = 0; i < arrInt5.Length; i++)
            {
                arrInt5[i] = i * 2 + 1;
            }

            for (int i = arrInt5.Length-1; i >=0; i--)
            {
                Console.WriteLine(arrInt5[i]);
            }

            Console.ReadKey();

            int[] arrInt;
            arrInt = new int[10];
            for (int i = 0; i < arrInt.Length/2; i++)
            {
                arrInt[i] = i;
            }
            arrInt[0] = 1;
            arrInt[8] = 8;

            for (int i = 0; i < arrInt.Length; i++)
            {
                Console.Write(arrInt[i] + "  ");
            }
            int[] arrayInt1 = { 1, 2, 3, 4 };
            int[] arrayInt2 = new int[4] { 1, 2, 3, 4 };
            int[] arrayInt3 = new int[4];
            for (int i = 0; i < arrayInt3.Length; i++)
            {
                arrayInt3[i] = i;
            }
            arrayInt3[1] = 5;

            Console.ReadKey();
        }

        private static void SortArray(int[] array)
        {
            ;
        }

        private static void TestArray3()
        {
            int[] ia = new int[10];
            for (int i = 0; i < 9; i++)
            {
                ia[i] = 2 * i * i + 1;
            }

            for (int i = 0; i < 10; i++)
            {
                Console.Write(ia[i] + " ");

            }

            int t = 8;
            ia[9] = t;
            for (int i = ia.Length-1; i >=0 ; i--)
            {
                if (ia[i] < ia[i - 1])
                {
                    t = ia[i];
                    ia[i] = ia[i - 1];
                    ia[i - 1] = t;
                }
                else
                    break;
            }
            Console.WriteLine();
            for (int i = 0; i < 10; i++)
            {
                Console.Write(ia[i] + " ");
            }

            Console.ReadKey();
        }

        private static void TestArray2()
        {
            string con = "";
            do
            {
                Console.WriteLine("Please Enter a Num:");
                int f1;
                while (!int.TryParse(Console.ReadLine(), out f1))
                {
                    Console.WriteLine("Input Error! Please Enter a Num:");
                }

                Console.WriteLine("输入的十进制数：{0}", f1);
                int temp = f1;
                Console.Write("二进制：");
                do
                {
                    Console.Write(temp % 2);
                    temp = temp / 2;
                } while (temp != 0);
                Console.WriteLine();

                temp = f1;

                Console.Write("八进制：");
                do
                {
                    Console.Write(temp%8);
                    temp = temp / 8;
                } while (temp != 0);
                Console.WriteLine();

                temp = f1;
                Console.Write("十六进制：");
                do
                {
                    int t = temp % 16;
                    if (t < 10)
                        Console.Write(t);
                    else
                        Console.Write((char)((t-10)+'A'));
                    temp /= 16;
                } while (temp != 0);


                Console.WriteLine("是否继续？（Y/N）");
                con = Console.ReadLine();
            } while (con == "Y" || con == "y");


            Console.ReadKey();
        }

        private static void TestArray1()
        {
            Random ran = new Random();
            float[,] fArray = new float[5, 5];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    fArray[i, j] =(float) ran.NextDouble()*1000;
                }
            }

            float max = fArray[0, 0];
            int h = 0, v = 0;
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Console.Write("[{0}][{1}]=>{2}    ", i, j, fArray[i, j]);
                    if (max < fArray[i, j])
                    {
                        max = fArray[i, j];
                        h = i;
                        v = j;
                    }
                }
                Console.WriteLine(); 
            }
            Console.WriteLine("[{0}][{1}]=>{2}    is Max Value.", h, v, fArray[h, v]);
            Console.ReadKey();
        }

        private static void TestArray()
        {
            int[,] arr1 = new int[10, 10];
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < i+1; j++)
                {
                    if (j == 0 || j == i)
                        arr1[i, j] = 1;
                    else
                        arr1[i, j] = arr1[i - 1,j - 1] + arr1[i - 1,j];
                    Console.Write(arr1[i, j] + "   ");
                }
                Console.WriteLine();
            }
            Console.ReadKey();

            int[,] arr = { {1,2,3,4 }, {5,6,7,8 }, {9,10,11,12 }, {13,14,15,16 } };
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Console.Write(arr[i, j] + " ");
                }
                Console.WriteLine();
            }
            int temp = 0;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    temp = arr[i,j];
                    arr[i, j] = arr[j, i];
                    arr[j, i] = temp;
                }
            }
            Console.WriteLine();
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Console.Write(arr[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}
